//
//  AWSTableviewAdapter.m
//  AWSBase
//
//  Created by AlanWang on 2018/2/26.
//  Copyright © 2018年 AlanWang. All rights reserved.
//

#import "JFTableviewAdapter.h"
#import <objc/objc.h>
#import "JFBase.h"
@interface JFTableviewAdapter()
@property(nonatomic,assign) NSUInteger sectionNumber;
@end
@implementation JFTableviewAdapter

-(BOOL)registerCellWithMoldelClass:(UITableView*)tableview modelClassName:(NSArray*)modelClassArr cellClassName:(NSString*)cellClassNameOrXibName{
    if (!tableview && !cellClassNameOrXibName) {
        return NO;
    }
    for (Class modelClass in modelClassArr) {
        NSString * modelClassStr = NSStringFromClass(modelClass);
        id  cell  = [tableview dequeueReusableCellWithIdentifier:modelClassStr];
        if(!cell){
            if([self isExistXibInBundleWithName:cellClassNameOrXibName]){
                [tableview registerNib:[UINib nibWithNibName:cellClassNameOrXibName bundle:nil] forCellReuseIdentifier:modelClassStr];
            }else{
                Class tempClass = NSClassFromString(cellClassNameOrXibName);
                if (tempClass) {
                     [tableview registerClass:tempClass forCellReuseIdentifier:modelClassStr];
                }else{
                    return NO;
                }
            }
        }
    }
    return YES;
}
//判断是不是xib
- (BOOL)isExistXibInBundleWithName:(NSString *)xibName
{
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *pathXib   = [bundle pathForResource:xibName ofType:@"nib"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isExistFile           = [fileManager fileExistsAtPath:pathXib];
    return isExistFile;
}

-(instancetype)init{
    self = [super  init];
    if (self) {
        self.sectionNumber = 1;
    }
    return  self;
}
//cell的代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.numberOfSections) {
        return self.numberOfSections(tableView);
    }else{
        return self.sectionNumber;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(self.numberOfRows){
      return  self.numberOfRows(tableView, section);
    }else{
        if (self.data.count) {
            id  tempData = self.data;
            if (tableView.numberOfSections != 0) {
                tempData = self.data[section];
            }
            return [self rowS:tempData];
        }else{
            return 0;
        }

    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.cellForRowAtIndexPath) {
        UITableViewCell * cell = self.cellForRowAtIndexPath(tableView,indexPath);
        if (!cell) {
            cell = [self getCell:indexPath table:tableView];
        }
        return cell;
    }else{
//        id tempdata =  [self rowVaule:indexPath];
//        Class tempclass = [tempdata class];
//        NSString * modelClassStr = NSStringFromClass(tempclass);
//        id  cell  = [tableView dequeueReusableCellWithIdentifier:modelClassStr];
//        if(cell){
//            if ([cell respondsToSelector:@selector(cellConfigVaule:indexPath:)]) {
//                [cell cellConfigVaule:tempdata indexPath:indexPath];
//            }
//            return cell;
//        }else{
//            if([tempdata respondsToSelector:@selector(identifier)]){
//                NSString * ident = [tempdata identifier];
//                id  cell  = [tableView dequeueReusableCellWithIdentifier:ident];
//                if (cell) {
//                    if ([cell respondsToSelector:@selector(cellConfigVaule:indexPath:)]) {
//                        [cell cellConfigVaule:tempdata indexPath:indexPath];
//                    }
//                    return cell;
//                }else{
//                    if([tempdata respondsToSelector:@selector(identifierClass)]){
//                        Class  tempclass = [tempdata identifierClass];
//                        id cell = [[tempclass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[tempdata identifier]];
//                        if(cell){
//                            if ([cell respondsToSelector:@selector(cellConfigVaule:indexPath:)]) {
//                                [cell cellConfigVaule:tempdata indexPath:indexPath];
//                            }
//                            return cell;
//                        }else{
//                            NSCAssert(1 == 2, @"未配置cell,且数据无法定位展示的cell请检查cell");
//                            return nil;
//                        }
//                    }else{
//                        NSCAssert(1 == 2, @"未配置cell,且数据无法定位展示的cell请检查cell");
//                        return nil;
//                    }
//                }
//            }else{
//                NSCAssert(1 == 2, @"未配置cell,且数据无法定位展示的cell请检查cell");
//                return nil;
//            }
//        }
        
       return  [self getCell:indexPath table:tableView];
    }
}

-(UITableViewCell*)getCell:(NSIndexPath*)indexPath table:(UITableView*)tableView{
    id tempdata =  [self rowVaule:indexPath];
    Class tempclass = [tempdata class];
    NSString * modelClassStr = NSStringFromClass(tempclass);
    id  cell  = [tableView dequeueReusableCellWithIdentifier:modelClassStr];
    if(cell){
        if ([cell respondsToSelector:@selector(cellConfigVaule:indexPath:)]) {
            [cell cellConfigVaule:tempdata indexPath:indexPath];
        }
        
        if (self.cellWillReturnBlock) {
            self.cellWillReturnBlock(tableView, indexPath, cell);
        }
        return cell;
    }else{
        if([tempdata respondsToSelector:@selector(identifier)]){
            NSString * ident = [tempdata identifier];
            id  cell  = [tableView dequeueReusableCellWithIdentifier:ident];
            if (cell) {
                if ([cell respondsToSelector:@selector(cellConfigVaule:indexPath:)]) {
                    [cell cellConfigVaule:tempdata indexPath:indexPath];
                }
                if (self.cellWillReturnBlock) {
                    self.cellWillReturnBlock(tableView, indexPath, cell);
                }
                return cell;
            }else{
                if([tempdata respondsToSelector:@selector(identifierClass)]){
                    Class  tempclass = [tempdata identifierClass];
                    id cell = [[tempclass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[tempdata identifier]];
                    if(cell){
                        if ([cell respondsToSelector:@selector(cellConfigVaule:indexPath:)]) {
                            [cell cellConfigVaule:tempdata indexPath:indexPath];
                        }
                        if (self.cellWillReturnBlock) {
                            self.cellWillReturnBlock(tableView, indexPath, cell);
                        }
                        return cell;
                    }else{
                        NSCAssert(1 == 2, @"未配置cell,且数据无法定位展示的cell请检查cell");
                        return nil;
                    }
                }else{
                    NSCAssert(1 == 2, @"未配置cell,且数据无法定位展示的cell请检查cell");
                    return nil;
                }
            }
        }else{
            NSCAssert(1 == 2, @"未配置cell,且数据无法定位展示的cell请检查cell");
            return nil;
        }
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.didSelectRowBlock) {
        self.didSelectRowBlock(tableView, indexPath);
    }
}
-(void)setData:(NSArray *)data{
    if (data && [data isKindOfClass:[NSArray class]] && data.count > 0) {
       _data = data;
       id  tempData = self.data[0];
       if(tempData && [tempData isKindOfClass:[NSArray class]]){
           self.sectionNumber = self.data.count;
       }else{
           self.sectionNumber = 1;
       }
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.heightForRow){
        return self.heightForRow(tableView,indexPath);
    }else{
        id tempdata =  [self rowVaule:indexPath];
        if([tempdata respondsToSelector:@selector(identifierClass)]){
            Class  tempclass = [tempdata identifierClass];
            if (tempclass) {
                SEL sel = @selector(cellHeightWithVaule:);
                if ([tempclass respondsToSelector:sel]) {
                    double height  = [tempclass cellHeightWithVaule:tempdata];
                    return height;
                }else{
                    if([tempdata respondsToSelector:@selector(cellHeight)]){
                        return [tempdata cellHeight];
                    }else{
                        return 0.0f;
                    }
                }
            }else{
                if([tempdata respondsToSelector:@selector(cellHeight)]){
                    return [tempdata cellHeight];
                }else{
                    return 0.0f;
                }
            }
            
//            IMP imp = [tempclass methodForSelector:@selector(cellHeightWithVaule:)];
//            if (imp) {
//                typedef double (*_myImp)(id target,SEL,...);
//                _myImp action = (_myImp)imp;
//                double  result = action(tempclass, @selector(cellHeightWithVaule:),tempdata);
//                return result;
//            }else{
//                if([tempdata respondsToSelector:@selector(cellHeight)]){
//                    return [tempdata cellHeight];
//                }else{
//                    return 0.0f;
//                }
//            }
        }else{
            
            if([tempdata respondsToSelector:@selector(cellHeight)]){
                return [tempdata cellHeight];
            }else{
                return 0.0f;
            }
        }
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.heightForSectionHeader) {
        return self.heightForSectionHeader(tableView,section);
    }else{
        return 0.1f;
    }
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.sectionHeaderView) {
        return self.sectionHeaderView(tableView,section);
    }else{
        return [UIView new];
    }
}
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (self.sectionFooterView) {
        return self.sectionFooterView(tableView,section);
    }else{
        return [UIView new];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (self.heightForSectionFooter) {
        return self.heightForSectionFooter(tableView,section);
    }else{
        return 0.1;
    }
}
#pragma mark--辅助方法
-(NSUInteger)rowS:(id)data{
    if (data) {
        if ([data isKindOfClass:[NSArray class]]) {
            NSArray * tempData = (NSArray*)data;
            return tempData.count;
        }else{
            return self.data.count;
        }
    }else{
        return 0;
    }
}
-(id)rowVaule:(NSIndexPath *)indexPath{
    id tempVaule = nil;
    id  data = nil;
    data = self.data[indexPath.section];
    if (data) {
        if ([data isKindOfClass:[NSArray class]] || [@"__NSArrayI" isEqualToString:NSStringFromClass([data class])] ||  [@"__NSArrayM" isEqualToString:NSStringFromClass([data class])]) {
            NSArray * tempData = (NSArray*)data;
            tempVaule =  tempData[indexPath.row];
        }else{
            tempVaule = self.data[indexPath.row];
        }
    }
    return tempVaule;
}
@end
